import * as React from 'react'; import { PhoneNumberField, Alert } from '@aws-amplify/ui-react'; import { PhoneNumberFieldDemo } from './demo'; import { Example, ExampleCode } from '@/components/Example'; import { Fragment } from '@/components/Fragment'; import { ComponentClassTable } from '@/components/ComponentClassTable'; import StandardHTMLAttributes from '@/components/StandardHTMLAttributes.mdx'; import { ComponentStyleDisplay } from '@/components/ComponentStyleDisplay'; import ThemeExample from '@/components/ThemeExample.mdx'; import { AccessibilityExample, AutoCompleteExample, DialCodeSelectExample, ClassNameExample, DefaultPhoneNumberFieldExample, DescriptiveTextExample, RefsExample, RequiredFieldExample, RequiredFieldStyledExample, SizeExample, StatesExample, StylePropsExample, PhoneNumberFieldThemeExample, ValidationErrorExample, VariationExample, } from './examples'; ## Demo ## Usage Import the `PhoneNumberField` component and provide a `label` for accessibility/usability as well as a `defaultDialCode` which will auto-populate the dial code select field. ```jsx file=./examples/DefaultPhoneNumberFieldExample.tsx ```` ### Dial Code Select Properties All `countryCode` fields are being deprecated in favor of the new `dialCode` fields. If you still have instances of `defaultCountryCode`, `countryCodeList`, `countryCodeName`, `countryCodeLabel`, and `onCountryCodeChange` then please update these to the new `dialCode` props listed below. The dial code selector can be customized by setting several properties when using the `PhoneNumberField` primitive. The custom properties specific to the dial code selector are the following: - `defaultDialCode` (_required_): The default dial code that will be selected upon render - `dialCodeList`: An array of dial codes (strings) used as options in the dial code selector - `dialCodeName`: A name used when handling form submission for the dial code selector - `dialCodeLabel`: A hidden accessible label for the dial code selector - `onDialCodeChange`: A custom change handler for the dial code selector * If both `defaultDialCode`, `dialCodeList`, `dialCodeName`, `dialCodeLabel`, and `onDialCodeChange` and the corresponding `countryCode` prop are provided, then the value in the `dialCode` prop will be preferred. ```jsx file=./examples/DialCodeSelectExample.tsx ```` ### Autocomplete - supporting password managers Use the `autoComplete` prop to tell the browser how to populate the `PhoneNumberField`. By default, the `PhoneNumberField` primitive uses `tel-national` as the `autoComplete` property for the text field and `tel-country-code` as the `autoComplete` property for the dial code selector. If the `PhoneNumberField` primitive is intended to be used in a form that is compatible with most password managers, the `autoComplete` property should be set to `username` (see [Password Form Styles that Chromium Understands](https://www.chromium.org/developers/design-documents/form-styles-that-chromium-understands/)). ```jsx file=./examples/AutoCompleteExample.tsx ```` ### Accessibility / Label behavior {() => import('./../shared/formFieldAccessibility.mdx')} ```jsx file=./examples/AccessibilityExample.tsx ```` ### Sizes `PhoneNumberField` sizes are designed to match the styling of other form field components such as Buttons. There are three sizes: `'small'`, (default), and `'large'`. ```jsx file=./examples/SizeExample.tsx ```` ### Variations There are two variation styles available: default and `quiet`. ```jsx file=./examples/VariationExample.tsx ```` ### Descriptive text To provide additional descriptive text of the field's requirements, use the `descriptiveText` prop. To customize the descriptive text, you may pass a `Text` component as the prop's value. ```jsx file=./examples/DescriptiveTextExample.tsx ```` ### States The available `PhoneNumberField` states include `isDisabled` and `isReadOnly`. A disabled field will be not be focusable, mutable, or submitted with form data. A readonly field cannot be edited by the user. ```jsx file=./examples/StatesExample.tsx ```` ### Required field Use the `isRequired` prop to specify a required field. If a user attempts to submit a field that is both required and empty, they will be prompted to fill out the field. ```jsx file=./examples/RequiredFieldExample.tsx ```` There is no default styling for required form fields. Customize the `label` or `descriptiveText` to instruct the user of the required field. ```jsx file=./examples/RequiredFieldStyledExample.tsx ```` ### Validation error Use the `hasError` and `errorMessage` fields to mark a `PhoneNumberField` as having a validation error. ```jsx file=./examples/ValidationErrorExample.tsx ```` ### Forward refs {() => import('./../shared/forwardRefAlert.mdx')} The standard `ref` prop will forward to the underlying `input` element, and the `dialCodeRef` prop forwards to the dial code `select` element. The following is a contrived example demonstrating use of the `ref` and `dialCodeRef` props: ```jsx file=./examples/RefsExample.tsx ```` ```jsx ``` ## CSS Styling ```tsx file=./examples/PhoneNumberFieldThemeExample.tsx ``` ### Target classes ### Global styling To override styling on all `PhoneNumberField` primitives, you can set the Amplify CSS variables with the built-in `.amplify-phonenumberfield` and `.amplify-dialcodeselect` class. ```css /* styles.css */ .amplify-phonenumberfield, .amplify-dialcodeselect { --amplify-components-fieldcontrol-border-color: rebeccapurple; } ``` ### Local styling To override styling on a specific `PhoneNumberField`, you can use a class selector or style props. _Using a class selector:_ ```css /* styles.css */ .custom-phonenumberfield-class .amplify-phonenumberfield, .custom-phonenumberfield-class .amplify-dialcodeselect { border-radius: 0; } ``` _Using style props:_ All style props will be applied to the [`Flex`](flex) wrapper of the `PhoneNumberField`. To style the `input` of the `PhoneNumberField`, you can pass a `inputStyles` prop with the style props you want to apply to the input. ```jsx file=./examples/StylePropsExample.tsx ```